const express = require("express");
const mutipart = require("connect-multiparty");
let mutipartMiddware = mutipart();
const uploads = express.Router();
uploads.use(mutipart({ uploadDir: "./uploads/assets" }));
uploads.all("*", function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Headers", "Content-Type");
  res.header("Access-Control-Allow-Headers", "mytoken");
  next();
});
// 文件上传接口
uploads.post("/fileUpload", mutipartMiddware, function (req, res) {
  // 上传的文件在req.files中
  console.log("req.files",req.files);
  // let files = req.files.null;
  // let path = req.files.null.path;
  // let size = req.files.null.size;
  const { name,path,size,type} =  req.files.file
  console.log("文件名称",name);
  console.log("文件路径",path);
  console.log("文件大小",size);
  console.log("文件类型",type)
  if(req.files) {
    res.send({ msg: "文件上传成功!" });
  } else {
    res.send({ msg: "文件上传失败" });
  }
});

/**
 * 文件下载接口
 */
 uploads.post('/download',function(req,res){
    res.download("./apk/base.apk",err=>{
        if(!err){
        res.send({msg:"下载成功!"});
        }else{
            console.log({ msg:"下载失败!"});
        }
    });
});
// 使用module.exprots导出路由对象
module.exports = uploads;
